
angular.module('dressModule', [])
	.controller('dressCtrl', function($scope,$state,dress,$ionicSideMenuDelegate,$timeout,$http,$ionicScrollDelegate) {
		$scope.router=$state.params;
		var a=1
		dress.getDressData()
			.then(function(data) {
				$scope.dress=data.data.data.list
				
			})
		$scope.btnClick = function(){
			$ionicSideMenuDelegate.toggleLeft();
		}
		$scope.isshow=false
		
//		$scope.scrollTop=function(){
//			
//			 $ionicScrollDelegate.scrollTop();
//		}
		$scope.loadMore = function(){
		console.log("正在请求更多");
		a++
		$http.get("/icy/m.php?fromPageId=0&pageSize=20&method=goods.getListByTag&sort=0&tag=早春美衣&page="+a)
				.then(function(data) {
					function transform(obj){
						    var arr = [];
						    for(var item in obj){
						        arr.push(obj[item]);
						    }
						    return arr;
						}
   
			$scope.dress=transform($scope.dress).concat(transform(data.data.data.list))
				});

		$timeout(function(){
			$scope.$broadcast('scroll.infiniteScrollComplete');
		},2000);
	}
	})
	.factory('dress', function($http,$state) {
		console.log($state.params);	
		var getDressData = function() {

			return $http.get("/icy/m.php?fromPageId=0&pageSize=20&method=goods.getListByTag&sort=0&page=1",{
				params:{
					tag:$state.params.tag
				}
			})
				.then(function(data) {
					return data;
				});
		};
		
		return {
			getDressData: getDressData
		};

	})
	.directive('hdb',function($rootScope,$ionicScrollDelegate){
    return {
        restrict: 'A',
        link: function($scope, element, attributes) {
//       console.dir(element[0].childNodes[0])
			element[0].onscroll = function(){
			console.log(element[0].scrollTop)
//			console.dir(this.childNodes[0])
			if (this.scrollTop >160) {
				$scope.$apply(function(){
					$scope.$parent.isshow= true;
				});
			} else{
					$scope.$apply(function(){
					$scope.$parent.isshow= false;
				});
			}
		}
			$scope.$parent.topClick =function(){
				console.log(1)
				$ionicScrollDelegate.scrollTop();
			}
        }
    };
})
